﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZK.YHP_TransManage.Test
{
    public partial class 文字转语音 : Form
    {

        SpeechSynthesizer voice = new SpeechSynthesizer();
        public 文字转语音()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            voice.Rate = 0; //设置语速,[-10,10]
            voice.Volume = 100; //设置音量,[0,100]

            //下面的代码为一些SpeechSynthesizer的属性，看实际情况是否需要使用
            string str = richTextBox1.Text;
            //voice.SpeakAsyncCancelAll();  //取消朗读
            voice.SpeakAsync(str);  //同步朗读
                                          // voice.Pause();  //暂停朗读
                                          // voice.Resume(); //继续朗读



        }

        private void button2_Click(object sender, EventArgs e)
        {
            SpeechSynthesizer speech = new SpeechSynthesizer();
            var dialog = new SaveFileDialog();
            dialog.Filter = "*.mp3|*.mp3";
            dialog.ShowDialog();

            string path = dialog.FileName;
            if (path.Trim().Length == 0)
            {
                return;
            }
            speech.SetOutputToWaveFile(path);
           // speech.SetOutputToDefaultAudioDevice();
            speech.Volume = 100;
            speech.Rate = -1;
            speech.Speak(richTextBox1.Text);
            speech.SetOutputToNull();
            MessageBox.Show("生成成功!在" + path + "路径中！", "提示");
        }
    }
}
